/*
 * MainState.cpp
 *
 *  Created on: 24-nov.-2013
 *      Author: Axel
 */

#include "MainState.h"
#include <stdlib.h>

#include "extraMath.h"
#include "render.h"
#include "Transform.h"



const char* MainState::pVS = "                                                      \n\
#version 330                                                                       \n\
                                                                                    \n\
layout (location = 0) in vec3 position;												\n\
uniform mat4 gWorld; 		                                                                            \n\
																					\n\
void main()                                                                         \n\
{                                                                                   \n\
    gl_Position =vec4(position, 1.0) * gWorld;												\n\
}";

const char* MainState::pFS = "                                                      \n\
#version 330                                                                      \n\
                                                                                    \n\
                                                                                    \n\
out vec3 FragColor;                                                                 \n\
                                                                                    \n\
void main()                                                                         \n\
{                                                                                   \n\
	FragColor = vec3(0.2,0.6,0.2);                                                  \n\
}";

MainState::MainState(void) {
	changeState = 0;
	hasBeenLoaded = false;

	ShaderProgram = 0;
	chars = new CharacterManager();

	loadshader = NULL;
}


MainState::~MainState(void) {
	glDeleteProgram(ShaderProgram);
}

void MainState::loadShader(const char* vertex, const char* fragment) {
	loadshader->compileShaders(vertex, fragment, ShaderProgram);
	glUseProgram(ShaderProgram);
	delete loadshader;
}

void MainState::init() {
	loadshader = new LoadShader();
	ShaderProgram = glCreateProgram();
	loadShader(pVS, pFS);
	initObjects();
}

void MainState::initObjects() {
	cam = new Camera();
	chars->addPlayer(0,0,0.2,0.2,0.01,60);
}

void MainState::handleInput(GLFWwindow* window) {
	if (glfwGetKey(window,GLFW_KEY_ESCAPE) == GLFW_PRESS) {
		glfwTerminate();
		exit(-1);
	}
	if (glfwGetKey(window,GLFW_KEY_A) == GLFW_PRESS) {
		changeState = 200000000L;
	}
	if (glfwGetKey(window,GLFW_KEY_UP) == GLFW_PRESS) {
		(chars->getPlayer())->move();
	}
	if (glfwGetKey(window,GLFW_KEY_LEFT) == GLFW_PRESS) {
		(chars->getPlayer())->turn(1);
	}
	if (glfwGetKey(window,GLFW_KEY_RIGHT) == GLFW_PRESS) {
		(chars->getPlayer())->turn(-1);
	}
	if (glfwGetKey(window,GLFW_KEY_DOWN) == GLFW_PRESS) {
		cam->zoom(-0.01);
	}
}

void MainState::updateGame(int timer) {

}

void MainState::renderGame(float interpolation) {
	cam->setPerspective(90.0f, 800, 800,0.1f, 100.0f);
	glUniformMatrix4fv(gWorldLocation, 1, GL_TRUE, (const GLfloat*)cam->getCamera());

	chars->render();

	gWorldLocation = glGetUniformLocation(ShaderProgram, "gWorld");
}

long MainState::stateChanger() {
	return changeState;
}

void MainState::reInit() {
	changeState=0;

	glUseProgram(ShaderProgram);

	if (!hasBeenLoaded) {
		init();
	}

	hasBeenLoaded=true;
}

long MainState::getKeyCode() {
	return keycode;
}
